package com.starbaba.module.view;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import com.base.common.database.BaseDao;
import com.base.common.exception.AppException;
import com.base.common.log.LogService;
import com.base.common.util.StringUtils;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.starbaba.core.utils.PropertyParamMapBuilder;
import com.starbaba.core.utils.Struts2Utils;
import com.starbaba.core.utils.json.JsonUtils;
import com.starbaba.core.web.page.PageResponse;
import com.starbaba.core.web.struts2.BaseAction;
import com.starbaba.model.dictionary.Constant;
import com.starbaba.module.model.LoanContentActivity;
import com.starbaba.module.model.ActivityModel;
import com.starbaba.module.service.ILoanContentActivityService;
import com.starbaba.redis.JedisUtils;

@SuppressWarnings({"unchecked", "rawtypes","deprecation"})
public class LoanContentActivityAction extends BaseAction implements ModelDriven<LoanContentActivity> {

    private static final long serialVersionUID = 1L;

    final BaseDao dao = new BaseDao(Constant.POOL_LIFE);

	private LoanContentActivity model;

	private ILoanContentActivityService service;

	@Autowired
	@Qualifier("loanContentActivityServiceImpl")
	public void setService(ILoanContentActivityService service) {
		this.service = service;
	}

	@Override
	public LoanContentActivity getModel() {
		if (model == null) {
			model = new LoanContentActivity();
		}
		return model;
	}

	public void getActivityList() {
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		parsePageRequest();
        PageResponse pageResponse = new PageResponse();

		int start = pageRequest.getIDisplayStart();
		int length = pageRequest.getIDisplayLength();
		List list = service.getActivityList(start, length, pageResponse, paramsMap);
		pageResponse.setAaData(list);
		pageResponse.setSEcho(pageRequest.getSEcho());
		try {
			sendJsonMessage(JsonUtils.toJson(pageResponse, null, true, null, false));
		} catch (IOException e) {
			LogService.error(e);
		}
	}

	public String detail() {
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		String id = (String) paramsMap.get("id");
		if (!StringUtils.isEmpty(id)) {
			String sql = "select * from loan_content_activity where id =?";
			try {
				LoanContentActivity activity = dao.get(sql, LoanContentActivity.class, id);
				paramsMap.put("activity", activity);
			} catch (AppException e) {
				e.printStackTrace();
			}
		}
		getModelList();
		return ActionSupport.SUCCESS;
	}

	public void getModelList() {
		String sql = "select id,modelName from activity_model";
		try {
			List<ActivityModel> list = dao.list(sql, ActivityModel.class, new Object[] {});
			paramsMap.put("modelList", list);
		} catch (AppException e) {

			e.printStackTrace();
		}
	}

	public void saveActivity() {
	    paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		String id = (String) paramsMap.get("id");
		Long mid = model.getMid();
		try {
			ActivityModel activityModel = dao.get("select * from activity_model where id =?", ActivityModel.class, mid);
			model.setTarget_url(activityModel.getTarget_url());
			if (!StringUtils.isEmpty(id)) {
				dao.update(model, new String[] { "id" });
				JedisUtils.del("activity_model_" + id);
			} else {
				model.setCtime(new Date());
				dao.add(model);
			}

		} catch (AppException e) {
			e.printStackTrace();
		}
	}
}